package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgCompany;
import com.myteam.fugaocrm.modle.FgPosition;
import com.myteam.fugaocrm.modle.FgStaff;
import com.myteam.fugaocrm.service.CompanyServiceImpl;
import com.myteam.fugaocrm.service.PositionServiceImpl;
import com.myteam.fugaocrm.service.StaffServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class StaffController {
	@Resource protected StaffServiceImpl staffServiceImpl;
	@Resource protected CompanyServiceImpl companyServiceImpl;
	@Resource protected PositionServiceImpl positionServiceImpl;
	
	@RequestMapping("staff/all")
	@ResponseBody
	public ModelAndView all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<FgStaff> fgStaffList = staffServiceImpl.all();
		
		List<FgCompany> fgCompanyList = companyServiceImpl.all();//全部公司
		List<FgPosition> fgPositionList = positionServiceImpl.all();//全部职务
		
		request.getSession().setAttribute("fgCompanyList", fgCompanyList);
		request.getSession().setAttribute("fgPositionList", fgPositionList);
		
		ModelAndView view = new ModelAndView();
		view.addObject("fgStaffList", fgStaffList);
		view.setViewName("arch_staff.jsp");
		return view;
	}
	
	@RequestMapping("staff/delete")
	@ResponseBody
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer staffId = Integer.parseInt(request.getParameter("staffId").toString());
		
		Integer loginStaffId = Integer.parseInt(request.getSession().getAttribute("staffId").toString());
		
		if(staffId == loginStaffId) {//TODO 不允许删除自己
			
		}
		
		staffServiceImpl.deleteOne(staffId);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/staff/all");
		return view;
	}
	
	@RequestMapping("staff/insertorupdate")
	@ResponseBody
	public ModelAndView insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String staffIdStr = request.getParameter("staffId")+"";
		String staffName = request.getParameter("staffName")+"";
		String positionIdStr = request.getParameter("positionId")+"";//职务
		String companyIdStr = request.getParameter("companyId")+"";//公司
		String permissionStr = request.getParameter("permission");//权限
		
		//校验
		
		Integer positionId = Integer.parseInt(positionIdStr);
		Integer companyId = Integer.parseInt(companyIdStr);
		Integer permission = 0;
		if(permissionStr == null) {
			FgStaff fgStaff = staffServiceImpl.selectStaffByStaffId(Integer.parseInt(staffIdStr));
			permission = Integer.parseInt(fgStaff.getPermission());
		}else {
			permission = Integer.parseInt(permissionStr);
		}
		
		//插入或更新
		Date date = new Date();
		Map<String, Object> params = new HashMap<String, Object>();
		
		if("".equals(staffIdStr)) {
			//insert
//			params.put("staffName", staffName);
//			params.put("createdDate", date);
//			params.put("createdUser", staffName);
//			params.put("modifiedDate", date);
//			params.put("modifiedUser", staffName);
//			staffServiceImpl.insert(params);
		}else {
			//update
			Integer staffId = Integer.parseInt(staffIdStr);
			params.put("staffId", staffId);
			params.put("positionId", positionId);
			params.put("companyId", companyId);
			params.put("permission", permission);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			staffServiceImpl.update(params);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/staff/all");
		return view;
	}
}
